package com.good.domain.service.impl;

import com.aliyun.dysmsapi20170525.models.SendSmsRequest;
import com.aliyun.dysmsapi20170525.models.SendSmsResponse;
import com.aliyun.dysmsapi20170525.models.SendSmsResponseBody;
import com.good.domain.service.ISmsService;
import org.springframework.beans.factory.annotation.Value;
import com.aliyun.teaopenapi.models.Config;
import org.springframework.stereotype.Service;

import java.io.ObjectInputFilter;

/**
 * @author：大钊
 * @data:2022/9/22 16:21
 * 发送短信
 **/
@Service
public class SmsServiceImpl implements ISmsService {
    @Value("${sms.accessKeyId}")
    private String accessKeyId;
    @Value("${sms.accessKeySecret}")
    private String accessKeySecret;
    @Value("${sms.signName}")
    private String signName;
    @Value("${sms.templateCode}")
    private String templateCode;
    @Value("${sms.endpoint}")
    private String endpoint;

    /**
     * 发送短信验证码
     * @param mobile 接收方手机号
     * @param code 验证码
     * @throws Exception
     */
    @Override
    public SendSmsResponseBody sendSmsCode(String mobile, String code) throws Exception {
        Config config = new Config()
                // 您的AccessKey ID
                .setAccessKeyId(accessKeyId)
                // 您的AccessKey Secret
                .setAccessKeySecret(accessKeySecret);
        // 访问的域名
        config.setEndpoint(endpoint);
        com.aliyun.dysmsapi20170525.Client client = new com.aliyun.dysmsapi20170525.Client(config);

        SendSmsRequest sendSmsRequest = new SendSmsRequest()
                .setSignName(signName)
                .setTemplateCode(templateCode)
                .setPhoneNumbers(mobile)
                .setTemplateParam("{\"code\":\""+code+"\"}");
        SendSmsResponse resp = client.sendSms(sendSmsRequest);
        return resp.getBody();

    }
}
